package com.example.demojenkins.controller;

import com.example.demojenkins.entity.TableData;
import com.example.demojenkins.entity.TblOrderInfo;
import com.example.demojenkins.entity.User;
import com.example.demojenkins.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2024-08-24 18:17:05
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<User>> queryByPage(User user, PageRequest pageRequest) {
        return ResponseEntity.ok(this.userService.queryByPage(user, pageRequest));
    }

    @PreAuthorize("hasAnyAuthority('text','admin')")
    @PostMapping(value = "/list")
    public Map<String, Object> queryByPage2(@RequestBody TableData tableData) {
        Integer page = tableData.getPageNum();
        Integer size = tableData.getPageSize();
        String orderName = tableData.getOrderName();
        if (page != null) {
            page = (page - 1) * size;
        }
        User user = new User();
        if(StringUtils.isNotEmpty(tableData.getOrderName())){
            user.setUserName(orderName);
        }
        Long count = userService.count(user);
        List<User> Users = userService.queryByPage2(page, size, orderName);
        Map<String, Object> map = new HashMap<>();
        map.put("total", count);
        map.put("data", Users);
        return map;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<User> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.userService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<User> add(User user) {
        return ResponseEntity.ok(this.userService.insert(user));
    }

    /**
     * 编辑数据
     *
     * @param
     * @return 编辑结果
     */
    @PreAuthorize("hasAnyAuthority('text','admin')")
    @PostMapping("/update")
    public ResponseEntity<User> edit(@RequestBody User user) {
        System.out.println(user);
        return ResponseEntity.ok(this.userService.update(user));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.userService.deleteById(id));
    }

}

